cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _iptAccount:null,
        _iptPassword:null,
    },

    // use this for initialization
    onLoad: function () {
        var btnLogin = cc.find("Canvas/page_login/btn_login");
        cc.addClickEvent(btnLogin,this.node,"PageLogin","onBtnClicked");
        
        this._iptAccount = cc.find("Canvas/page_login/ipt_account").getComponent(cc.EditBox);
        this._iptPassword = cc.find("Canvas/page_login/ipt_pswd").getComponent(cc.EditBox);
        
        var account = cc.sys.localStorage.getItem("account");
        var password = cc.sys.localStorage.getItem("password");
        
        if(account){
            this._iptAccount.string = account;
        }
        if(password){
            this._iptPassword.string = password;
        }
        if(account && password){
            this.doLogin();
        }
    },
    
    doLogin:function(){
        var fn = function(ret){
            if(ret.errcode == 0){
                var info = this.node.getComponent("UserInfo");
                info.setUserInfo(ret.headimg,ret.name,ret.gems);
                cc.userdata = ret;
                this.node.emit("switch_page","wjck");
            }
            else{
                cc.sys.localStorage.removeItem("account");
                cc.alert.show("登陆失败","错误的用户名或密码");
            }
        };
        
        var data = {
            account:this._iptAccount.string,
            password:this._iptPassword.string,
        }
        cc.sys.localStorage.setItem("account",data.account);
        cc.sys.localStorage.setItem("password",data.password);
        cc.http.sendRequest("/login",data,fn.bind(this));
    },

    onBtnClicked:function(event){
        this.doLogin();
    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
